<!--
 * @Author: lirl
 * @Date: 2022-11-03 14:37:36
 * @Description: 起草页面
-->
<template>
  <div>
    <jdf-select-identity
      ref="selectIdentity"
      v-model="identityId"
      :identity-list="identityList"
      @choose="draftProcess"
      notice-text="请选择人员身份"
    ></jdf-select-identity>
  </div>
</template>

<script>
import { mapGetters } from 'vuex';
import { window } from 'globalthis/implementation';
import { ProcessService, UserIdentityService } from 'jdf-jssdk';
import { isEmpty } from 'lodash-es';
import { bizServiceURL } from '@/api/utils/constants';
import { showLoading } from '@/utils/utils';
// import switchIdentItiesMixin from '@/mixins/switchIdentItiesMixin';

export default {
  // mixins: [switchIdentItiesMixin],
  data() {
    const { templateCode, processDefinitionKey } = this.$route.query;
    return {
      // 身份列表
      identityList: [],
      identityId: '',
      templateCode,
      processDefinitionKey
    };
  },
  computed: {
    ...mapGetters(['loginName'])
  },
  created() {
    this.draft();
  },
  methods: {
    /**
     * 起草流程
     */
    async draft() {
      // 获取当前人身份列表
      const identityList = await new UserIdentityService(bizServiceURL).getByLoginName(this.loginName);
      // 如果没有身份，则弹出提示
      if (isEmpty(identityList)) {
        this.$alert('未获取到起草人身份', '提示', {
          type: 'warning',
          callback: () => {
            this.closeWindow();
          }
        });
      }
      // 如果只有一个身份，则直接起草
      if (identityList.length === 1) {
        const [{ id }] = identityList;
        this.identityId = id;
        this.draftProcess();
      } else {
        // 如果有多个身份，则弹出选身份窗口
        this.identityList = identityList;
        this.$refs.selectIdentity.show();
      }
    },
    /**
     * 关闭窗口
     */
    closeWindow() {
      window.open('', '_self');
      window.close();
    },
    /**
     * 起草流程方法
   T * @param {String} templateId 流程模板id
     * @param {String} title 待办标题
     * @param {String} userIdentifyId 起草人身份id，可不传
     * @param {Object} additionalData 附加数据 要通过路由的query传给业务表单的数据，非必传
     * @param {String} to 要跳转到的路由地址
     */
    async draftProcess() {
      const loading = showLoading('流程发起中，请稍候...');
      try {
        const { templateCode } = this;
        const data = await new ProcessService(bizServiceURL).draft({
          identityId: this.identityId,
          handlerIdentityId: [],
          processDefinitionKey: templateCode,
          title: '暂无标题',
        });
        this.$router.push({
          path: '/process-form',
          query: {
            ...this.$route.query,
            processDefinitionKey: templateCode,
            taskId: data
          }
        });
      } catch (error) {
        this.$alert(error?.message, '提示', {
          callback: () => {
            this.closeWindow();
          }
        });
      } finally {
        loading.close();
      }
    }
  }
};
</script>

<style lang="scss">
.identity-list {
  .el-radio-group {
    .el-radio {
      display: block;
      &:not(:first-child) {
        margin-top: 10px;
      }
    }
  }
}
.jdf-select-identity {
  .el-dialog {
    width: 500px;
  }
}
.notice-text {
  margin-bottom: 20px;
}
</style>
